package com.ftwj.service;

import com.ftwj.entity.SchoolPlaceInfo;
import com.ftwj.entity.SchoolRoomSeatInfo;
import com.ftwj.mapper.HomePageMapper;
import com.ftwj.mapper.SchoolRoomSeatMapper;
import com.ftwj.mapper.UserAppointmentInfoMapper;
import com.ftwj.vo.UserAppointmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class HomePageService {
    @Autowired
    private HomePageMapper homePageMapper;

//    public Map<String, Double> getReservationRatio() {
//        List<SchoolPlaceInfo> places = homePageMapper.getAllPlaces();
//        Map<String, Double> reservationRatioMap = new HashMap<>();
//
//        for (SchoolPlaceInfo place : places) {
//            List<SchoolRoomSeatInfo> seats = homePageMapper.getSeatsByPlaceId(place.getId());
//            long totalSeats = seats.size();
//            long reservedSeats = homePageMapper.getReservedSeatsByPlaceId(place.getId()).size();
//
//            double ratio = totalSeats > 0 ? ((double) reservedSeats / totalSeats) * 100 : 0;
//            reservationRatioMap.put(place.getName(), ratio);
//        }
//
//        return reservationRatioMap;
//    }
    public List<SchoolPlaceInfo> getReservationRatio() {
        List<SchoolPlaceInfo> places = homePageMapper.getAllPlaces();
        List<SchoolPlaceInfo> result = new ArrayList<>();

        for (SchoolPlaceInfo place : places) {
        List<SchoolRoomSeatInfo> seats = homePageMapper.getSeatsByPlaceId(place.getId());
        long totalSeats = seats.size();
        long reservedSeats = homePageMapper.getReservedSeatsByPlaceId(place.getId()).size();

        double ratio = totalSeats > 0 ? ((double) reservedSeats / totalSeats) * 100 : 0;

        // 更新SchoolPlaceInfo的reservationRatio属性
        place.setReservationRatio(ratio);
        // 更新SchoolPlaceInfo的totalSeats属性
        place.setTotalSeats(totalSeats);
        result.add(place);
     }

     return result;
    }
    /**
     * 统计近15天每个场所每天的预约人数
     * @return 预约统计信息列表
     */
    public List<Map<String, Object>> countAppointmentsByPlaceAndDayInLast15Days() {
        // 获取当前时间前15天的日期
        LocalDate startDate = LocalDate.now().minusDays(15);
        // 调用MyBatis映射器执行SQL查询
        List<Map<String, Object>> statistics = homePageMapper.countAppointmentsByPlaceAndDayInLast15Days(startDate.plusDays(15));
        return statistics;
    }

    public List<UserAppointmentVO> getAppointmentDetails() {

        return homePageMapper.getAppointmentDetails();
    }

}
